cmake_minimum_required(VERSION 3.10)

project(rk_c++)


set(CMAKE_BUILD_TYPE Debug)
# set(CMAKE_BUILD_TYPE Release)

if (ENABLE_ASAN)
	message(STATUS "BUILD WITH ADDRESS SANITIZER")
	set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
	set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
	set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif ()



# 指定项目的源文件
# add_executable(${PROJECT_NAME}
#     src/det/det_main.cc
#     src/det/det_process.cc
#     src/det/det.cc
# )

# # 指定项目的源文件
# add_executable(${PROJECT_NAME}
#     src/keypoint/keypoint_main.cc
#     src/keypoint/keypoint_process.cc
#     src/keypoint/keypoint.cc
# )

# # 指定项目的源文件
# add_executable(${PROJECT_NAME}
#     src/cls/cls_main.cc
#     src/cls/cls_process.cc
#     src/cls/cls.cc
# )

# 指定项目的源文件
add_executable(${PROJECT_NAME}
    src/track/track_det.cpp
    src/track/colormap_parser.cpp
    src/track/opencv_utils.cpp
    src/det/det.cc
    src/det/det_process.cc

)


# # 添加第三方库
set(THIRD_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rd_party)

# 设置 json 头文件和库文件路径
set(JSON_INCLUDE_DIRS "${THIRD_PATH}/json/include")
include_directories(${JSON_INCLUDE_DIRS})

# 包括 Eigen 头文件
set(EIGEN_INCLUDE_DIRS "${THIRD_PATH}/eigen")
include_directories(${EIGEN_INCLUDE_DIRS})

# 设置 BYTETRACK 头文件和库文件路径
set(BYTETRACK_INCLUDE_DIRS "${THIRD_PATH}/bytetrack/include")
set(BYTETRACK_LIB_DIR "${THIRD_PATH}/bytetrack/lib")
include_directories(${BYTETRACK_INCLUDE_DIRS})
link_directories(${BYTETRACK_LIB_DIR})


# 设置 OpenCV 头文件和库文件路径
set(OpenCV_INCLUDE_DIRS "${THIRD_PATH}/opencv/include/opencv4")
set(OpenCV_LIB_DIR "${THIRD_PATH}/opencv/lib")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})

# stb image
set(STB_PATH ${THIRD_PATH}/stb_image)
set(STB_INCLUDES ${STB_PATH}/include )

# jpeg turbo
set(JPEG_PATH ${THIRD_PATH}/jpeg_turbo)
set(LIBJPEG ${JPEG_PATH}/lib/libturbojpeg.a )
set(LIBJPEG_INCLUDES ${JPEG_PATH}/include )

# rknn runtime
set(RKNN_PATH ${THIRD_PATH}/rknpu2)
set(LIBRKNNRT ${RKNN_PATH}/lib/librknnrt.so)
set(LIBRKNNRT_INCLUDES ${RKNN_PATH}/include )

# rga
set(RGA_PATH ${THIRD_PATH}/librga)
set(LIBRGA ${RGA_PATH}/lib/librga.so )
set(LIBRGA_INCLUDES ${RGA_PATH}/include )


# 自定义库
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/utils/include
)

add_library(fileutils STATIC
    utils/file_utils.c
)

add_library(imagedrawing STATIC
    utils/image_drawing.c
)

add_definitions(-DLIBRGA_IM2D_HANDLE)
add_library(imageutils STATIC
    utils/image_utils.c
)

target_link_libraries(imageutils
    ${LIBJPEG}
    ${LIBRGA}
)

target_include_directories(imageutils PUBLIC
    ${STB_INCLUDES}
    ${LIBJPEG_INCLUDES}
    ${LIBRGA_INCLUDES}
)


# 链接 OpenCV ByteTrack 库
# file(GLOB OpenCV_LIBS "${OpenCV_LIB_DIR}/*.so")
# file(GLOB ByteTrack_LIBS "${BYTETRACK_LIB_DIR}/*.so")
file(GLOB OpenCV_LIBS "${OpenCV_LIB_DIR}/*")
file(GLOB ByteTrack_LIBS "${BYTETRACK_LIB_DIR}/*")
target_link_libraries(${PROJECT_NAME}
    fileutils
    imageutils
    imagedrawing   
    ${LIBRKNNRT}
    ${LIBTRACK}
    ${OpenCV_LIBS}
    ${ByteTrack_LIBS}
)

# 添加项目的include目录
target_include_directories(${PROJECT_NAME} PUBLIC
    ${LIBRKNNRT_INCLUDES}
    include/det
    include/keypoint
    include/cls
    include/track
)

set(CMAKE_INSTALL_RPATH "${PROJECT_SOURCE_DIR}/install")
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install")

install(TARGETS ${PROJECT_NAME} DESTINATION )
install(PROGRAMS ${LIBRGA} DESTINATION lib)
install(PROGRAMS ${LIBRKNNRT} DESTINATION lib)
install(PROGRAMS ${OpenCV_LIBS} DESTINATION lib)
install(PROGRAMS ${ByteTrack_LIBS} DESTINATION lib)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/model/
        DESTINATION model
        FILES_MATCHING
        PATTERN "*")




